package com.hong.concurrent.chapter7;

/**
 * @Author csh
 * @Description 不可变对象性能测试
 * @Date 2018/7/3 20:58
 */
public class ImmutablePerformance {

    public static void main(String[] args) throws InterruptedException {

        //23415
        long startTimestamp2 = System.currentTimeMillis();
        SyncObj syncObj = new SyncObj("Alex");
        Thread t1 = new Thread() {
            @Override
            public void run() {
                for (long l = 0L; l < 1000000; l++) {
                    System.out.println(Thread.currentThread().getName() + "=" + syncObj.toString());
                }
            }
        };
        t1.start();
        Thread t2 = new Thread() {
            @Override
            public void run() {
                for (long l = 0L; l < 1000000; l++) {
                    System.out.println(Thread.currentThread().getName() + "=" + syncObj.toString());
                }
            }
        };

        t2.start();
        t1.join();
        t2.join();

        long endTimestamp2 = System.currentTimeMillis();
        //4310
        System.out.println("Elapsed time "+(endTimestamp2-startTimestamp2));

        /*long startTimestamp = System.currentTimeMillis();
        SyncObj syncObj = new SyncObj("Alex");
        for (long l=0L;l<1000000;l++){
            System.out.println(syncObj.toString());
        }
        long endTimestamp = System.currentTimeMillis();
        //5780
        System.out.println("Elapsed time "+(endTimestamp-startTimestamp));*/
        //21302
        /*long startTimestamp2 = System.currentTimeMillis();
        ImmutableObj immutableObj = new ImmutableObj("Alex");
        Thread t1 = new Thread() {
            @Override
            public void run() {
                for (long l = 0L; l < 1000000; l++) {
                    System.out.println(Thread.currentThread().getName() + "=" + immutableObj.toString());
                }
            }
        };
        t1.start();
        Thread t2 = new Thread() {
            @Override
            public void run() {
                for (long l = 0L; l < 1000000; l++) {
                    System.out.println(Thread.currentThread().getName() + "=" + immutableObj.toString());
                }
            }
        };

        t2.start();
        t1.join();
        t2.join();

        long endTimestamp2 = System.currentTimeMillis();
        //4310
        System.out.println("Elapsed time "+(endTimestamp2-startTimestamp2));*/
    }
}

class ImmutableObj{
    private final String name;

    public ImmutableObj(String name) {
        this.name = name;
    }

    @Override
    public String toString() {
        return "ImmutableObj{" +
                "name='" + name + '\'' +
                '}';
    }
}

class SyncObj{
    private String name;

    public SyncObj(String name) {
        this.name = name;
    }

    public synchronized void setName(String name) {
        this.name = name;
    }

    @Override
    public synchronized String toString() {
        return "SyncObj{" +
                "name='" + name + '\'' +
                '}';
    }
}
